Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qml.grouping.is_pauli_word returns false for non-observables #3039

Merged
merged 3 commits into from
Sep 8, 2022

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Sep 8, 2022

For PennyLane Lightning PR #349, I am trying to get adjoint differentiation working with operator arithmetic observables.

On that PR branch, I try:

dev = qml.device('lightning.qubit', wires=2)

@qml.qnode(dev, diff_method="adjoint")
def circuit(x):
    qml.RX(x, wires=0)
    return qml.expval(qml.s_prod(2.0, qml.PauliZ(0)))

x = qml.numpy.array(1.0)

circuit(x)

Only to get the error (tail end of traceback only):

File ~/Prog/pennylane-lightning/pennylane_lightning/_serialize.py:66, in _obs_has_kernel(ob)
     57 def _obs_has_kernel(ob: Observable) -> bool:
     58     """Returns True if the input observable has a supported kernel in the C++ backend.
     59 
     60     Args:
   (...)
     64         bool: indicating whether ``obs`` has a dedicated kernel in the backend
     65     """
---> 66     if is_pauli_word(ob):
     67         return True
     68     if isinstance(ob, (Hadamard, Projector)):

File ~/Prog/pennylane/pennylane/grouping/utils.py:77, in is_pauli_word(observable)
     53 """
     54 Checks if an observable instance is a Pauli word.
     55 
   (...)
     73 False
     74 """
     76 if not isinstance(observable, Observable):
---> 77     raise TypeError(f"Expected {Observable} instance, instead got {type(observable)} instance.")
     79 pauli_word_names = ["Identity", "PauliX", "PauliY", "PauliZ"]
     80 if isinstance(observable, Tensor):

TypeError: Expected <class 'pennylane.operation.Observable'> instance, instead got <class 'pennylane.ops.op_math.sprod.SProd'> instance.

This occurs because we no longer only measure observables. Sum, Prod, and SProd are only plain Operator's.

So we simply return False if the provided object is not a Pauli Word. Non-observables are obviously not Pauli Words.

This may also improve PR #3033 .

Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've searched GitHub and all code that invokes is_pauli_word raises a TypeError when it returns False anyway (with the exception of the accused _obs_has_kernel in various plugins), so this makes good sense to me. It logically follows too: "Is my random non-observable operator a pauli word?" "no". lgtm

Copy link
Contributor

@Jaybsoni Jaybsoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a simple enough change. I imagine this code will have to get changed again in the future when we integrate op_math

@codecov
Copy link

codecov bot commented Sep 8, 2022

Codecov Report

Merging #3039 (53556dc) into master (74ba19e) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #3039   +/-   ##
=======================================
  Coverage   99.66%   99.66%           
=======================================
  Files         269      269           
  Lines       22892    22892           
=======================================
  Hits        22815    22815           
  Misses         77       77           
Impacted Files Coverage Δ
pennylane/grouping/utils.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@albi3ro albi3ro merged commit 481683c into master Sep 8, 2022
@albi3ro albi3ro deleted the is_pauli_word_on_nonobservable branch September 8, 2022 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants